package com.yzy.fly.mapper;

import com.yzy.fly.entity.role.FlyServiceMethod;
import com.yzy.fly.kernel.mapper.FlyBasicMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;


@Mapper
public interface FlyServiceMethodMapper extends FlyBasicMapper<FlyServiceMethod> {
    @Select("select * from fly_service_method a where a.id = #{id}")
    FlyServiceMethod selectById(@Param("id") int id);

    @Select("select * from fly_service_method a where 1=1")
    List<FlyServiceMethod> selectAll();

    @Select("select * from fly_service_method a where 1=1 and a.serviceId = #{serviceId} order by a.seq asc")
    List<FlyServiceMethod> selectAllByServiceId(@Param("serviceId") int serviceId);

    @Select("select * from fly_service_method a where 1=1 and a.systemId = #{systemId}")
    List<FlyServiceMethod> selectAllBySystemId(@Param("systemId") int systemId);

//    @Select({"select * from fly_service_method a where 1=1 and a.classId = (select b.fieldId from fly_field_annotation b where b.id = #{annotationId})",
//    "","",""})
    @Select({"<script>",
            "select * ",
            "from fly_service_method a",
            "where 1=1",
            "<when test='classId!=null'>",
            "AND a.classId = #{classId}",
            "</when>",
            "<when test='systemId!=null'>",
            "AND a.systemId = #{systemId}",
            "</when>",
            "</script>"})
    List<FlyServiceMethod> selectAllByParentId(@Param("classId") Integer classId, @Param("systemId") Integer systemId);

    @Select("select * from fly_service_method a where 1=1 and a.name = #{name}")
    FlyServiceMethod selectOneByName(@Param("name") String name);

    @Delete("delete a from fly_service_method a where 1=1 and a.parentId = #{serviceId}")
    int deleteAllByServiceId(@Param("serviceId")Integer id);

    /**
     * 批量插入（mysql）
     * @param entityList
     * @return
     */
    Integer insertBatchSomeColumn(Collection<FlyServiceMethod> entityList);


    @Select({"<script>",
            "select max(a.seq) from fly_service_method a where 1=1 and ",
            "<choose>",
            "<when test='parentId == null'>",
            "parentId is null ",
            "</when>",
            "<otherwise>",
            "parentId = #{parentId} ",
            "</otherwise>",
            "</choose>",
            "and componentId = #{componentId}",
            "</script>"})
    Integer maxSeqNo(@Param("parentId")Integer parentId,@Param("componentId")int componentId);
}